<template>
  <div class="login">
    <div class="login-bg">
      <img src="@/assets/team-building-animation.svg" alt="团建背景" class="team-building-bg" />
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">团建预约系统</h3>
      <div class="form-decoration">
        <svg class="form-decoration-svg" width="50" height="50" viewBox="0 0 50 50">
          <circle cx="25" cy="25" r="20" fill="none" stroke="#1890ff" stroke-width="1.5" stroke-dasharray="100">
            <animate attributeName="stroke-dashoffset" from="100" to="0" dur="3s" repeatCount="indefinite" />
          </circle>
          <path d="M15,25 L22,32 L35,18" fill="none" stroke="#1890ff" stroke-width="1.5" stroke-linecap="round" stroke-dasharray="50">
            <animate attributeName="stroke-dashoffset" from="50" to="0" dur="1.5s" begin="0.5s" repeatCount="indefinite" />
          </path>
        </svg>
      </div>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          class="login-button"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
          <svg v-if="!loading" class="login-button-arrow" width="20" height="20" viewBox="0 0 20 20">
            <g transform="translate(10, 10)">
              <line x1="-5" y1="0" x2="5" y2="0" stroke="white" stroke-width="2" stroke-linecap="round">
                <animate attributeName="x2" values="5;7;5" dur="1s" repeatCount="indefinite" />
              </line>
              <polyline points="0,-3 5,0 0,3" fill="none" stroke="white" stroke-width="2" stroke-linecap="round">
                <animate attributeName="points" values="0,-3 5,0 0,3;0,-3 7,0 0,3;0,-3 5,0 0,3" dur="1s" repeatCount="indefinite" />
              </polyline>
            </g>
          </svg>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            // Cookies.set("username", this.loginForm.username)
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          Cookies.set("username", this.loginForm.username);

          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #e8f4f8 0%, #d7e9f7 100%);

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: radial-gradient(circle at 30% 50%, rgba(255,255,255,0.8) 0%, rgba(255,255,255,0.1) 70%);
    z-index: 1;
  }
}

.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;

  .team-building-bg {
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0.8;
    animation: fadeIn 1.5s ease-in-out;
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #3a5d7a;
  font-size: 24px;
  font-weight: bold;
  position: relative;
  animation: slideDown 0.8s ease-out;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.1);

  &::after {
    content: '';
    position: absolute;
    bottom: -10px;
    left: 50%;
    width: 50px;
    height: 3px;
    background: #1890ff;
    transform: translateX(-50%);
    border-radius: 3px;
    animation: expandWidth 1s ease-out forwards;
  }
}

.form-decoration {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 50px;
  height: 50px;
  opacity: 0.7;
  z-index: 3;

  .form-decoration-svg {
    width: 100%;
    height: 100%;
  }
}

.login-form {
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.9);
  width: 400px;
  padding: 30px 30px 15px 30px;
  position: relative;
  z-index: 2;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(5px);
  animation: fadeInUp 0.8s ease-out;
  border: 1px solid rgba(255, 255, 255, 0.5);

  .el-input {
    height: 38px;
    margin-bottom: 5px;
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-2px);
    }

    input {
      height: 38px;
      background: rgba(255, 255, 255, 0.8);
      border-color: #d8e2e9;
      transition: all 0.3s ease;

      &:focus {
        border-color: #1890ff;
        box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
      }
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
    color: #1890ff;
    transition: all 0.3s ease;
  }

  .el-form-item {
    margin-bottom: 22px;
    opacity: 0;
    animation: fadeInRight 0.5s ease-out forwards;

    &:nth-child(2) {
      animation-delay: 0.1s;
    }

    &:nth-child(3) {
      animation-delay: 0.2s;
    }

    &:nth-child(4) {
      animation-delay: 0.3s;
    }

    &:nth-child(5) {
      animation-delay: 0.4s;
    }
  }
}

.login-button {
  width: 100%;
  height: 40px;
  border-radius: 20px;
  background: linear-gradient(to right, #1890ff, #40a9ff);
  border: none;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
  box-shadow: 0 5px 15px rgba(24, 144, 255, 0.3);

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(24, 144, 255, 0.4);
    background: linear-gradient(to right, #40a9ff, #1890ff);
  }

  &:active {
    transform: translateY(1px);
  }

  .login-button-arrow {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
  }

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    animation: buttonShine 3s infinite;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 4px;
    transition: all 0.3s ease;

    &:hover {
      transform: scale(1.05);
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    }
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 0.8; }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInRight {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes expandWidth {
  from { width: 0; }
  to { width: 50px; }
}

@keyframes buttonShine {
  0% { left: -100%; }
  20% { left: 100%; }
  100% { left: 100%; }
}
</style>

